<template>
<div class="coupon-marketing views-container">
    <share-link :config="shareLinkConfig" ref="sharelink">
        <template slot="title">-优惠券</template>
        <template slot="el-form-item">
            <!-- <el-form-item label="页面链接" >
                <el-input style="width: 270px;" readonly placeholder="请输入内容" v-model="shareLinkConfig.params.qrUrl" class="input-with-select">
                    <el-button slot="append" @click="handleClipboard(shareLinkConfig.params.qrUrl,$event)">复制</el-button>
                </el-input>
            </el-form-item>
            <el-form-item label="直接弹出购买界面的链接">
                <el-input style="width: 270px;" readonly placeholder="请输入内容" v-model="shareLinkConfig.params.linkUrl" class="input-with-select">
                    <el-button slot="append" @click="handleClipboard(shareLinkConfig.params.linkUrl,$event)">复制</el-button>
                </el-input>
            </el-form-item> -->
            <el-form-item label="小程序路径" v-if="shareLinkConfig.params.qrUrl">
          <el-input style="width: 270px;" readonly placeholder="请输入内容" v-model="shareLinkConfig.params.qrUrl" class="input-with-select">
            <el-button slot="append" @click="handleClipboard(shareLinkConfig.params.qrUrl,$event)">复制</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="公众号链接" v-if="shareLinkConfig.params.linkUrl">
          <el-input style="width: 270px;" readonly placeholder="请输入内容" v-model="shareLinkConfig.params.linkUrl" class="input-with-select">
            <el-button slot="append" @click="handleClipboard(shareLinkConfig.params.linkUrl,$event)">复制</el-button>
          </el-input>
        </el-form-item>
         <el-form-item label="短链接" v-if="shareLinkConfig.params.weChat_short_url">
          <el-input style="width: 270px;" readonly placeholder="请输入内容" v-model="shareLinkConfig.params.weChat_short_url" class="input-with-select">
            <el-button slot="append" @click="handleClipboard(shareLinkConfig.params.weChat_short_url,$event)">复制</el-button>
          </el-input>
        </el-form-item>
        </template>
    </share-link>
    <div class="wlm-table no-bg">
        <div class="wlm-table-content">
            <el-carousel ref="carousel" arrow="never" class="wlm-carousel" :autoplay="false" indicator-position="none" trigger="click" height="130px">
                <el-carousel-item v-for="item in platformList" :key="item.name" :name="item.name">
                    <el-row :gutter="20">
                        <el-col :span="6">
                            <div class="plugin-item flex-row">
                                <div class="flex-col flex-justify-b scale-grow" style="position: relative;">
                                    <div>
                                        <div class="flex-row flex-justify-b">
                                            <div class="item-title">满减券</div>
                                            <div>
                                                <router-link tag="span" to="/application/couponMarketingFullAdd">
                                                    <el-button type="primary" size="small">立即创建</el-button>
                                                </router-link>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="item-text">满足优惠条件立享减免</div>
                                </div>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div class="plugin-item flex-row">
                                <div class="flex-col flex-justify-b scale-grow" style="position: relative;">
                                    <div>
                                        <div class="flex-row flex-justify-b">
                                            <div class="item-title">折扣券</div>
                                            <div>
                                                <router-link tag="span" to="/application/couponMarketingDiscountAdd">
                                                    <el-button type="primary" size="small">立即创建</el-button>
                                                </router-link>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="item-text">满足折扣立享折扣</div>
                                </div>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div class="plugin-item flex-row">
                                <div class="flex-col flex-justify-b scale-grow" style="position: relative;">
                                    <div>
                                        <div class="flex-row flex-justify-b">
                                            <div class="item-title">运费券</div>
                                            <div>
                                                <router-link tag="span" to="/application/couponMarketingFreightAdd">
                                                    <el-button type="primary" size="small">立即创建</el-button>
                                                </router-link>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="item-text">满足条件立享减免运费</div>
                                </div>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div class="plugin-item flex-row">
                                <div class="flex-col flex-justify-b scale-grow" style="position: relative;">
                                    <div>
                                        <div class="flex-row flex-justify-b">
                                            <div class="item-title">随机券</div>
                                            <div>
                                                <router-link tag="span" to="/application/couponMarketingRandomAdd">
                                                    <el-button type="primary" size="small">立即创建</el-button>
                                                </router-link>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="item-text">获得金额随机的优惠券</div>
                                </div>
                            </div>
                        </el-col>
                    </el-row>
                </el-carousel-item>
            </el-carousel>
        </div>
    </div>
    <div class="wlm-table">
        <!-- 筛选 -->
        <div class="wlm-table-header">
            <el-form :model="tableFormatData.couponmarketing.files" size="small" label-width="100px" class="retail-form" label-position="right">
                <el-form-item label="" class="filter">
                    <el-form-item label="优惠券名称：" class="search">
                        <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.couponmarketing.files.name" placeholder="请输入优惠券名称" style="width:160px;"></el-input>
                    </el-form-item>
                    <el-form-item label="优惠券类型：" class="search">
                        <el-select v-model="tableFormatData.couponmarketing.files.couponType" placeholder="请选择">
                            <el-option v-for="item in [{lable:'全部',value:'0'},{lable:'满减券',value:'10'},{lable:'折扣券',value:'20'},{lable:'运费券',value:'30'},{lable:'随机券',value:'40'}]" :key="item.value" :label="item.lable" :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="" class="filter-btns">
                    <el-button type="primary" @click="filesSerch">筛选</el-button>
                    <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!-- 优惠券列表 -->
        <div class="wlm-table-content">
            <el-tabs v-model="tableFormatData.couponmarketing.files.couponStatus" type="card" @tab-click="filesSerch">
                <el-tab-pane label="全部" name="0"></el-tab-pane>
                <el-tab-pane label="进行中" name="1"></el-tab-pane>
                <el-tab-pane label="已失效" name="2"></el-tab-pane>
                <el-tab-pane label="已停止" name="3"></el-tab-pane>
            </el-tabs>
            <el-table :ref="tableFormatData.couponmarketing.key" :data="tableFormatData.couponmarketing.tableData" @selection-change="handleSelectionChange" style="width: 100%">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column label="优惠券名称" >
                    <template slot-scope="scope">
                        <el-popover trigger="hover" placement="top">
                            <p>{{scope.row.name}}</p>
                            <div slot="reference" class="wlm-table-wrapper">
                                <p>{{scope.row.name}}</p>
                            </div>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="优惠券类型" >
                    <template slot-scope="scope">
                        <span>{{coupon_type[`${scope.row.coupon_type}`]}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="优惠券内容" >
                    <template slot-scope="scope">
                        <el-popover trigger="hover" placement="top">
                            <p>
                                <couponRender>
                                    <template slot="title" v-if="scope.row.coupon_type">
                                        {{`${scope.row.rule === 0 ? `${coupon_price[`${scope.row.coupon_type}`]['textRule']}${scope.row.rule_min_money}元`:''} ${coupon_price[`${scope.row.coupon_type}`]['textPrev']}`}}
                                    </template>
                                    <template slot="price">
                                        {{scope.row.coupon_type === 40 ? `${scope.row.min} - ${scope.row.max}` : scope.row.coupon_type === 20 ? scope.row.discount : scope.row.reduce_price}}
                                    </template>
                                    <template slot="tail" v-if="scope.row.coupon_type">
                                        {{coupon_price[`${scope.row.coupon_type}`]['textLast']}}
                                    </template>
                                </couponRender>
                            </p>
                            <div slot="reference" class="wlm-table-wrapper">
                                <couponRender>
                                    <template slot="title" v-if="scope.row.coupon_type">
                                        {{`${scope.row.rule === 0 ? `${coupon_price[`${scope.row.coupon_type}`]['textRule']}${scope.row.rule_min_money}元`:''} ${coupon_price[`${scope.row.coupon_type}`]['textPrev']}`}}
                                    </template>
                                    <template slot="price">
                                        {{scope.row.coupon_type === 40 ? `${scope.row.min} - ${scope.row.max}` : scope.row.coupon_type === 20 ? scope.row.discount : scope.row.reduce_price}}
                                    </template>
                                    <template slot="tail" v-if="scope.row.coupon_type">
                                        {{coupon_price[`${scope.row.coupon_type}`]['textLast']}}
                                    </template>
                                </couponRender>
                            </div>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="总数/已领取" >
                    <template slot-scope="scope">
                        <span>{{`${scope.row.total_num}/${scope.row.receive_num}`}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="已使用" >
                    <template slot-scope="scope">
                        <span>{{scope.row.useCount}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="状态" >
                    <template slot-scope="scope">
                        <span>{{coupon_status[`${scope.row.status}`]}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="支付金额" >
                    <template slot-scope="scope">
                        <span class="text-danger">{{scope.row.turnover}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="客单价" >
                    <template slot-scope="scope">
                        <span class="text-danger" v-if="scope.row.unitPrice">{{scope.row.unitPrice | numFilter}}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="操作"  width="290">
                    <template slot-scope="scope">
                        <div class="operation-group">
                            <router-link class="wlm-text" tag="div" :to="{path:'/application/couponMarketingRecords', query:{couponId:scope.row.coupon_id}}">
                                <el-button type="text" size="small">效果数据</el-button>
                            </router-link>
                            <div class="btn-line"></div>
                            <el-button v-if="scope.row.status == 1" class="wlm-text" type="text" @click="changeTableItem({couponId:scope.row.coupon_id,op:'stop'},tableFormatData.couponmarketing.change.stop)">停止</el-button>
                            <el-button v-if="scope.row.status === 3" class="wlm-text text-danger" type="text" @click="changeTableItem({couponId:scope.row.coupon_id,op:'go'},tableFormatData.couponmarketing.change.stop)">已停止</el-button>
                            <div class="btn-line"></div>
                            <router-link class="wlm-text" tag="div" :to="{path:coupon_type_path[`${scope.row.coupon_type}`], query :{ couponId : scope.row.coupon_id}}">
                                <el-button type="text">编辑</el-button>
                            </router-link>
                            <div class="btn-line"></div>
                            <el-button class="wlm-text" type="text" @click="sharelink(scope.row.coupon_id)">推广</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-content flex-row flex-justify-b flex-align-c">
                <el-row class="pagination-btns">
                    <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.couponmarketing.files.checkall" @change="toggleSelection">全部</el-checkbox>
                    <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
                </el-row>
                <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.couponmarketing.pagination.page" :page-sizes="tableFormatData.couponmarketing.pagination.pagesizes" :page-size.sync="tableFormatData.couponmarketing.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.couponmarketing.pagination.total : 0">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import {
  couponList,
  couponDelete,
  couponOp
} from '@/api/application'
import clipboard from '@/utils/clipboard'

import mixins from '@/mixins/mixins'
import ShareLink from '@/components/ShareLink/index'
export default {
  mixins: [mixins.getters('Table')],
  name: 'CouponMarketing',
  components: {
    ShareLink,
    couponRender: {
      template: '<span><slot name="title"></slot><span class="text-danger"><slot name="price"></slot></span><slot name="tail"></slot></span>'
    }
  },
  filters: {
    numFilter(value) {
      const tempVal = parseFloat(value).toFixed(3)
      const realVal = tempVal.substring(0, tempVal.length - 1)
      return realVal
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      filters: {
        addZero: function(data) {
          return data.toFixed(2)
        }
      },
      shareLinkConfig: {
        current: 'weChatQr',
        params: {
           weChat_short_url:'',
                    qrUrl: '',
                    linkUrl: '',
                    weChatQr: '',
                    appQr: ''
        }
      },
      platformList: [{
        name: 'shang',
        data: []
      }],
      coupon_status: {
        '1': '进行中',
        '2': '已失效',
        '3': '已停止'
      },
      coupon_type: {
        10: '满减券',
        20: '折扣券',
        30: '运费券',
        40: '随机券'
      },
      coupon_price: {
        10: {
          textRule: '满',
          textPrev: '减免',
          textLast: '元'
        },
        20: {
          textRule: '满',
          textPrev: '打',
          textLast: '折'
        },
        30: {
          textRule: '满',
          textPrev: '减免',
          textLast: '元'
        },
        40: {
          textRule: '满',
          textPrev: '范围内随机',
          textLast: '元'
        }
      },
      coupon_type_path: {
        10: '/application/couponMarketingFullAdd',
        20: '/application/couponMarketingDiscountAdd',
        30: '/application/couponMarketingFreightAdd',
        40: '/application/couponMarketingRandomAdd'
      },
      tableFormatData: {
        current: 'couponmarketing',
        couponmarketing: {
          key: 'couponmarketing',
          api: {
            getList: couponList,
            delList: couponDelete
          },
          tableData: [],
          files: {
            ids: [],
            name: '',
            checkall: '0',
            Recycle: '1',
            couponType: '0',
            couponStatus: '0',
            redirect: 'coupon_id'
          },
          change: {
            stop: {
              name: '停止',
              key: 'stop',
              api: couponOp
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    handleSelect(key, keyPath) {
      this.$refs.carousel.setActiveItem(key)
    },
     handleClipboard(text, event) {
                clipboard(text, event)
                },
    sharelink(couponId) {
      couponOp({ couponId: couponId, op: 'tg' }).then((response) => {
        const { data: { data: { weChat_qrcode, wxApp_qrcode, weChat_url, wxApp_url, url,weChat_short_url } } } = response
        this.shareLinkConfig.params.qrUrl = wxApp_url
                this.shareLinkConfig.params.linkUrl = weChat_url
                this.shareLinkConfig.params.weChatQr = weChat_qrcode
                this.shareLinkConfig.params.appQr = wxApp_qrcode
                this.shareLinkConfig.params.url = url
                this.shareLinkConfig.params.weChat_short_url = weChat_short_url
                this.shareLinkConfig.current = weChat_qrcode ? 'weChatQr' : weChat_qrcode ? 'appQr' : ''
      })
      this.shareLinkConfig.params.couponId = couponId
      this.$refs.sharelink.toggle()
    }
  }
}
</script>

<style lang="scss" scoped>
.coupon-marketing {
    font-size: 14px;

    .plugin-item {
        height: 130px;
        border: 1px solid rgb(236, 236, 236);
        background-color: #ffffff;
        padding: 24px;
        border-radius: 3px;

        .item-title {
            font-size: 16px;
            font-weight: 600;
        }

        .item-text {
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    }
}
</style>
